import { fileURLToPath, URL } from "node:url";
import { resolve } from 'node:path';
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          // 将所有带短横线的标签名都视为自定义元素
          isCustomElement: (tag) => tag.includes("-"),
        },
      },
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  build: {
    lib: {
      entry: resolve(__dirname, 'src/utils/register-wc.js'),
      name: "VueWebComponents",
      formats: ["es", 'umd', "cjs"],
      fileName: (format) => `vue-web-components.${format}.js`,
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: ["vue"],
      output: {
        // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
        globals: {
          vue: 'Vue' // 这意味着在全局环境中，Vue 库的变量名是 ‘Vue’
        }
      }
    },
    // 输出目录
    outDir: "dist",
    // 清空输出目录
    emptyOutDir: true,
  },
});
